package com.example.yu.circle_progress;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;

import com.example.yu.circle_progress.utils.DensityUtil;

public class CircleProgressView extends View {

    private int circle_color;
    private int progress_bar_color;
    private Paint circlePaint;
    private Paint progressBarPaint;

    public CircleProgressView(Context context) {
        super(context);
    }

    public CircleProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        //获取样式属性
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircleProgressView);
        circle_color = typedArray.getColor(R.styleable.CircleProgressView_circle_color, Color.GREEN);
        progress_bar_color = typedArray.getColor(R.styleable.CircleProgressView_progressbar_color, Color.RED);
        typedArray.recycle();
        initPaint();
    }

    private void initPaint() {
        circlePaint = new Paint();
        progressBarPaint = new Paint();
        circlePaint.setColor(circle_color);
        circlePaint.setAntiAlias(true);
        circlePaint.setStyle(Paint.Style.FILL);

        progressBarPaint.setColor(progress_bar_color);
        progressBarPaint.setStyle(Paint.Style.STROKE);
        progressBarPaint.setAntiAlias(true);
        progressBarPaint.setStrokeWidth(5);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int specModeWidth = MeasureSpec.getMode(widthMeasureSpec);//得到宽度的布局模式
        int specSizeWidth = MeasureSpec.getSize(widthMeasureSpec);//得到布局参数的宽度
        int resultWidth = specSizeWidth;
        switch (specModeWidth){
            case MeasureSpec.EXACTLY://宽高的值是固定的时候 如mathc_parent 或
                resultWidth = specSizeWidth;
                break;
                case MeasureSpec.AT_MOST://你的宽高的值 位 wrap_content的时候
                    resultWidth = DensityUtil.dip2px(getContext(),200);
                    break;

        }
        int specModeHeight = MeasureSpec.getMode(heightMeasureSpec);//得到高度的布局模式
        int specSizeHeight = MeasureSpec.getSize(heightMeasureSpec);//得到布局参数的高度
        int resultHeight = specSizeHeight;
        switch (specModeHeight) {
            case MeasureSpec.EXACTLY:// 宽高的值是固定的时候,如:match_parent 或 "22dp"
                resultHeight = specSizeHeight;
                break;
            case MeasureSpec.AT_MOST:// 你的宽高的值 为 wrap_content 的时候
                resultHeight = DensityUtil.dip2px(getContext(), 200);
                break;
        }
        //调用 该方法， 最终确定该View 的宽高 setMeasuredDimension(设置测量的维度
        setMeasuredDimension(resultWidth,resultHeight);

    }

    /**
     * 圆形进度条  首先把已知的属性写出来， 有一个逻辑顺序d
     */
    private int circleRadius = 0;
    private int progressBarRadius;
    private int cx;
     private int cy;
     private int flag = 0;

    /**
     * 绘画
     * @param canvas
     */
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onDraw(Canvas canvas) {
        int cx = getMeasuredWidth()/2;
        int cy = getMeasuredHeight()/2;
        progressBarRadius = Math.min(cx, cy);
        flag++;
       circleRadius= (int) (progressBarRadius*flag/360.0f);
        canvas.drawCircle(cx,cy,circleRadius-2,circlePaint);
        Path path = new Path();
        canvas.drawArc(4,4,cx*2-4,cy*2-4,0,flag,false,progressBarPaint);
        if(flag<360){
            postInvalidateDelayed(30);
        }


    }
}
